package main

import (
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
)

func main1() {
	router := gin.New()
	// router.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
	// 	// 用户IP、请求时间、请求方法、请求路径、状态码、耗时
	// 	return fmt.Sprintf("[myLogger] %v  %v %s %v %s %v %s %v %s %v",
	// 		params.TimeStamp.Format("2006-01-02 15:04:05"),
	// 		params.ClientIP,
	// 		params.MethodColor(), params.Method, params.ResetColor(),
	// 		params.Path,
	// 		params.StatusCodeColor(), params.StatusCode, params.ResetColor(),
	// 		params.Latency,
	// 	)
	// }))

	// file, _ := os.Create("gin.log")
	file, _ := os.OpenFile("./gin.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	defer file.Close()
	fileWriter := io.MultiWriter(file)
	router.Use(gin.LoggerWithConfig(gin.LoggerConfig{
		Formatter: func(params gin.LogFormatterParams) string {
			// 用户IP、请求时间、请求方法、请求路径、状态码、耗时
			return fmt.Sprintf("[myLogger] %v  %v  %v  %v  %v  %v\n",
				params.TimeStamp.Format("2006-01-02 15:04:05"),
				params.ClientIP,
				params.Method,
				params.Path,
				params.StatusCode,
				params.Latency,
			)
		},
		// 输出的地方
		Output: fileWriter,
		// 要排除记录日志的路由
		SkipPaths: []string{"/r1"},
	}))

	router.SetTrustedProxies([]string{"127.0.0.1"})
	router.GET("/r2", func(ctx *gin.Context) {
		ctx.String(http.StatusOK, "你好啊")
	})
	router.GET("/r1", func(ctx *gin.Context) {
		ctx.String(http.StatusOK, "你好啊")
	})
	router.Run("127.0.0.1:8003")
}
